﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml;

namespace GreenArrow
{
    class Marker
    {
        Bitmap screenBitmap;
        public List<ConditionPiece> pieces = new List<ConditionPiece>();
        public Point clickPoint;
        public int TotalWidth;
        public int TotalHeight;

        public Marker() { }
        public Marker(Bitmap _bitmap) {
            screenBitmap = _bitmap;
        }
        public void SetPieces(List<GRect> grects, Point pt) {
            int minX = 10000;
            int minY = 10000;
            int maxX = 0;
            int maxY = 0;
            foreach (GRect gRect in grects) {
                if (gRect.Left < minX) {
                    minX = gRect.Left;
                }
                if (gRect.Right > maxX)
                {
                    maxX = gRect.Right;
                }
                if (gRect.Top < minY)
                {
                    minY = gRect.Top;
                }
                if (gRect.Bottom > maxY)
                {
                    maxY = gRect.Bottom;
                }
            }
            if (pt.X < minX) {
                minX = pt.X;
            }
            if (pt.X > maxX)
            {
                maxX = pt.X;
            }
            if (pt.Y < minY)
            {
                minY = pt.Y;
            }
            if (pt.Y > maxY)
            {
                maxY = pt.Y;
            }
            TotalWidth = maxX - minX;
            TotalHeight = maxY - minY;
            foreach (GRect gRect in grects) {
                Bitmap pieceBmp = Arrow.CopyRectFromBitmap(screenBitmap, gRect.Left, gRect.Top, gRect.Width, gRect.Height);
                ConditionPiece cp = new ConditionPiece(pieceBmp, gRect.Left - minX, gRect.Top - minY, gRect.Width, gRect.Height);
                pieces.Add(cp);
            }
            clickPoint = new Point(pt.X - minX, pt.Y - minY);
        }
        public void Save(string markerDirectory) {
            for (int c = 0; c < pieces.Count; c++) {
                pieces[c].bitmap.Save(markerDirectory + "\\" + string.Format("{0:D4}", c + 1) + ".bmp");
            }
            XmlDocument xmldoc = new XmlDocument();
            XmlElement root = xmldoc.CreateElement("marker");
            xmldoc.AppendChild(root);
            XmlElement element;
            element = xmldoc.CreateElement("width");
            element.InnerText = TotalWidth.ToString();
            root.AppendChild(element);
            element = xmldoc.CreateElement("height");
            element.InnerText = TotalHeight.ToString();
            root.AppendChild(element);

            element = xmldoc.CreateElement("click_x");
            element.InnerText = clickPoint.X.ToString();
            root.AppendChild(element);
            element = xmldoc.CreateElement("click_y");
            element.InnerText = clickPoint.Y.ToString();
            root.AppendChild(element);
            XmlElement piecesXML = xmldoc.CreateElement("pieces");
            for (int c = 0; c < pieces.Count; c++)
            {
                XmlElement elementPiece = xmldoc.CreateElement("piece");

                element = xmldoc.CreateElement("width");
                element.InnerText = pieces[c].Width.ToString();
                elementPiece.AppendChild(element);

                element = xmldoc.CreateElement("height");
                element.InnerText = pieces[c].Height.ToString();
                elementPiece.AppendChild(element);

                element = xmldoc.CreateElement("relative_x");
                element.InnerText = pieces[c].RelativeX.ToString();
                elementPiece.AppendChild(element);

                element = xmldoc.CreateElement("relative_y");
                element.InnerText = pieces[c].RelativeY.ToString();
                elementPiece.AppendChild(element);

                element = xmldoc.CreateElement("bmp_filename");
                element.InnerText = string.Format("{0:D4}", c + 1) + ".bmp";
                elementPiece.AppendChild(element);

                piecesXML.AppendChild(elementPiece);
            }
            root.AppendChild(piecesXML);
            xmldoc.Save(markerDirectory + "\\marker.xml");
        }
        public static Marker Load(string markerDirectory)
        {
            Marker newMarker = new Marker();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(markerDirectory + "\\marker.xml");
            newMarker.TotalWidth = Int32.Parse(xmldoc.SelectSingleNode("/marker/width").InnerText);
            newMarker.TotalHeight = Int32.Parse(xmldoc.SelectSingleNode("/marker/height").InnerText);

            foreach (XmlNode node in xmldoc.SelectNodes("/marker/pieces/piece")) {
                int width = Int32.Parse(node.SelectSingleNode("width").InnerText);
                int height = Int32.Parse(node.SelectSingleNode("height").InnerText);
                int relative_x = Int32.Parse(node.SelectSingleNode("relative_x").InnerText);
                int relative_y = Int32.Parse(node.SelectSingleNode("relative_y").InnerText);
                string bmp_filename = node.SelectSingleNode("bmp_filename").InnerText;
                Bitmap bmp = new Bitmap(markerDirectory + "\\" + bmp_filename);
                newMarker.pieces.Add(new ConditionPiece(bmp, relative_x, relative_y, width, height));
            }
            int click_x = Int32.Parse(xmldoc.SelectSingleNode("/marker/click_x").InnerText);
            int click_y = Int32.Parse(xmldoc.SelectSingleNode("/marker/click_y").InnerText);
            newMarker.clickPoint = new Point(click_x, click_y);
            return newMarker;
        }
    }
}
